package com.bruce.controller;

import com.bruce.common.PageBean;
import com.bruce.common.Response;
import com.bruce.common.ResponseStatus;
import com.bruce.dto.SearchDTO;
import com.bruce.po.ResumePO;
import com.bruce.service.ResumeService;
import com.bruce.util.ObjectUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Liang.Yong.hui
 * @date 2021/7/3 21:51
 * @description
 */
@RestController
@RequestMapping("/api/demo")
public class ResumeAPICtl {

    @Resource
    private ResumeService resumeService;

    @RequestMapping(path = "/resume/add.do",method = RequestMethod.POST)
    public Response<Integer> add(@RequestBody ResumePO resumePO) {
        if (ObjectUtil.isNull(resumePO)) {
            return Response.fail(ResponseStatus.PARAMS_ERROR);
        }
        return resumeService.add(resumePO);
    }

    @RequestMapping(path = "/resume/update.do",method = RequestMethod.POST)
    public Response<Integer> update(@RequestBody ResumePO resumePO) {
        if (ObjectUtil.isNull(resumePO,resumePO.getId())) {
            return Response.fail(ResponseStatus.PARAMS_ERROR);
        }
        return resumeService.update(resumePO);
    }

    @RequestMapping(path = "/resume/search.do",method = RequestMethod.POST)
    public Response<PageBean<ResumePO>> serach(@RequestBody SearchDTO dto) {
        if (ObjectUtil.isNull(dto) || ObjectUtil.isNull(dto.getOffset())) {
            return Response.fail(ResponseStatus.PARAMS_ERROR);
        }
        if (ObjectUtil.isNull(dto.getLimit())) {
            dto.setLimit(4);
        }
        PageBean<ResumePO> pageBean = new PageBean<>();
        pageBean.setData(resumeService.search(dto));
        pageBean.setTotalCount(resumeService.count().getData());
        return Response.success(pageBean);
    }

    @RequestMapping(path = "/resume/delete.do",method = RequestMethod.GET)
    public Response<Integer> delete(String ids) {
        if (ObjectUtil.isNull(ids)) {
            return Response.fail(ResponseStatus.PARAMS_ERROR);
        }
        return Response.success(resumeService.delete(ids));
    }

}
